<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoute"
      :collapse="collapse"
      background-color="#324157"
      text-color="#bfcbd9"
      active-text-color="#ffd04b"
      unique-opened
      router
    >
      <template v-for="(item,index) in auth">
        <template v-if="item.subauthourities.length>0">
          <el-submenu :index="item.page+''" :key="item.id">
          <template slot="title">
                <i :class="icon[index]"></i>
                <span slot="title">{{item.name}}</span>
          </template>
          <template>
          <el-menu-item :index="'/'+subItem.page" v-for="subItem in item.subauthourities" :key="subItem.id">
            <i class="el-icon-menu"></i>
            <template slot="title">{{ subItem.name }}</template>
          </el-menu-item>
          </template>
          </el-submenu>
        </template>
	<template v-else>
	  <el-menu-item :index="'/'+item.page" :key="item.id">
	      <i :class="icon[index]"></i>
	      <span slot="title">{{item.name}}</span>
	  </el-menu-item>
	</template>
      </template>
    </el-menu>
  </div>
</template>

<script>
import bus from "../lib/bus";
import { authourityController } from "../api/index";
export default {
  data() {
    return {
      // 权限左侧数据
      auth: [],
      collapse: false,
      icon: [
        "el-icon-s-home",
        "el-icon-s-shop",
        "el-icon-s-order",
        "el-icon-s-marketing",
        "el-icon-s-management",
        "el-icon-s-custom",
        "el-icon-s-unfold",
        "el-icon-s-finance",
        "el-icon-s-platform",
        "el-icon-s-check",
        "el-icon-user-solid",
        "el-icon-question",
        "el-icon-picture",
        "el-icon-s-ticket",
        "el-icon-s-cooperation"
      ]
    };
  },
  watch:{
  },
  computed: {
    onRoute() {
        return this.$route.path;
    }
  },
  created() {
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on("collapse", msg => {
      this.collapse = msg;
      bus.$emit("collapse-content", msg);
    });
    // 获取权限数据
    this.getData();
  },
  methods: {
    // 获取权限数据
    async getData() {
      const res = await authourityController();
      console.log(res)
      this.auth = res.data.authourity.subauthourities
      // console.log(this.auth)
    }
  }
};
</script>

<style lang="less" scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.sidebar > ul {
  height: 100%;
}
</style>
